<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="closeDlg"
    :close-on-click-modal="false"
    destroy-on-close="true"
    style="margin-top: -60px"
    :width="width"
  >
    <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
<!--    <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;" height="200">-->
<!--      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" show-overflow-tooltip />-->
<!--    </el-table>-->
    <!--    对话框下方按钮-->
    <div slot="footer" class="dialog-footer">
      <!--    取消按钮-->
      <!--      <el-button @click="closeDlg">-->
      <!--        {{ $t('table.cancel') }}-->
      <!--      </el-button>-->

      <el-button type="warning" @click="closeDlg">
        关闭
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import global from '@/common'
import { batchCreateChainFrequencyData, UploadChainFrequencyData } from '@/api/weixing/chainFrequencyData'
import { getToken } from '@/utils/auth'

export default {
  name: 'ChainFreqDataUpExcel',
  components: { UploadExcelComponent },
  props: {
    width: {
      type: String,
      default: '650px'
    },
    height: {
      type: String,
      default: '280px'
    },
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '链路频率校准数据导入'
    },
    row: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      list: null, // 列表数据
      listLoading: true, // 列表状态为加载中
      loadingBtn: false, // 防止表单重复提交
      downloadLoading: false,
      loadingBtnTitle: '确定', // 提交按钮标题
      dialogStatus: 'create',
      listIcon: global.listIcon, // 列表按钮图标
      tableData: [],
      tableHeader: []
    }
  },
  created() {
    console.log(this.row)
    this.title = '链路频率校准数据导入【' + this.row.name + '】'
  },
  methods: {
    beforeUpload(file) {
      const isLt10M = file.size / 1024 / 1024 < 10

      if (isLt10M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    async handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
      // console.log(this.tableData)
      // console.log(this.tableHeader)
      let temp
      const arr = []
      this.tableData.map(data => {
        const obj = {}
        for (let i = 0; i < this.tableHeader.length; i++) {
          eval('temp=data.' + this.tableHeader[i])
          switch (i) {
            case 0:
              obj.ID = temp
              break
            case 1:
              obj.orderSeq = temp
              break
            case 2:
              obj.centerFrequency = temp
              break
            case 3:
              obj.bandWidth = temp
              break
            case 4:
              obj.coupling = temp
              break
            case 5:
              obj.gain = temp
              break
            case 6:
              obj.loss = temp
              break
          }
        }
        arr.push(obj)
        // console.warn(obj)
      })
      console.log(arr)
      const res = await UploadChainFrequencyData({ token: getToken(), data: arr })
      console.log(res)
      if (res.header.code === 0) {
        this.$notify({
          title: '导入成功',
          message: '导入成功',
          type: 'success',
          duration: global.notify.durationShort
        })
        this.closeDlg()
      } else {
        this.loadingBtn = false // 提交按钮可用
        this.loadingBtnTitle = '确定'
        this.$notify({
          title: '导入失败',
          message: res.header.msg,
          type: 'error',
          duration: global.notify.duration
        })
      }
    },
    closeDlg() {
      this.visible = false
      this.$parent.chainFreqDataExcelVisible = false
    }
  }
}
</script>

<style scoped>

</style>
